package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.utils.FocusUtil;
import com.ohos.carbon.LogUtils;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;import static ohos.multimodalinput.event.TouchEvent.*;

public class SeekBarSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title;
    private Slider slider, slider3, slider2, slider4;
    private Slider slider1;
    int index = 0;
    int id = 0;
    private int pointX;
    private static int mButtonX = 0;
    private static int mButtonX2 = 0;
    private static int mButtonX3 = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_seek_bar);
        initView();
        initRangeSeekBar();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("SeekBar")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("SeekBar")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("SeekBar", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("SeekBar", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("SeekBar", 1);
            pf.flushSync();
        }

    }

    private void initRangeSeekBar() {
        slider = (Slider) findComponentById(ResourceTable.Id_slider);
        slider2 = (Slider) findComponentById(ResourceTable.Id_slider2);
        slider4 = (Slider) findComponentById(ResourceTable.Id_slider3);
        slider3 = (Slider) findComponentById(ResourceTable.Id_slider4);
        Text text1 = (Text) findComponentById(ResourceTable.Id_progresstext);
        Text text2 = (Text) findComponentById(ResourceTable.Id_progress);
        Text progresstext3 = (Text) findComponentById(ResourceTable.Id_progresstext3);
        String str = String.format("%.1f", (float) slider.getProgress() / 20);
        text1.setText(str);
        String str1 = String.format("%.1f", (float) slider3.getProgress() / 20);
        progresstext3.setText(str1);
        String str2 = String.format("%.1f", (float) slider.getProgress() / 20);
        text2.setText(str2);
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                String str = String.format("%.1f", (float) slider.getProgress() / 20);
                text1.setText(str);
                text1.setContentPositionX(slider.getProgress() * 1010 / 100);
            }

            @Override
            public void onTouchStart(Slider slider) {
                text1.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onTouchEnd(Slider slider) {
                text1.setVisibility(Component.INVISIBLE);
            }
        });

        slider4.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                String str = String.format("%.1f", (float) slider4.getProgress() / 20);
                text2.setText(str);
                text2.setContentPositionX(slider4.getProgress() * 1010 / 100);
            }

            @Override
            public void onTouchStart(Slider slider) {
                text2.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onTouchEnd(Slider slider) {
                text2.setVisibility(Component.INVISIBLE);
            }
        });

        slider2.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                String str = String.format("%.1f", (float) slider.getProgress() / 20);
                text1.setText(str);
                text1.setContentPositionX(slider.getProgress() * 1010 / 100);
            }

            @Override
            public void onTouchStart(Slider slider) {
                text1.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onTouchEnd(Slider slider) {
                text1.setVisibility(Component.INVISIBLE);
            }
        });

        Button button = (Button) findComponentById(ResourceTable.Id_button);
        button.setTouchEventListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case PRIMARY_POINT_DOWN:
                    slider1.setEnabled(false);
                    break;
                case POINT_MOVE:
                    pointX = FocusUtil.getWx(touchEvent) - 107;
                    button.setContentPositionX(pointX);
                    slider1.setProgressValue(slider1.getProgress());
                    index = pointX;
                    mButtonX = pointX;
                    if (id == 0) {
                        id = index;
                    }
                    LogUtils.d("Darren","slider1.getProgress():" + slider1.getProgress());
                    if (pointX <= slider1.getProgress()) {
                        button.setContentPositionX(694);
                        mButtonX = 694;
                        slider1.setViceProgress(694);
                    } else {
                        if (id > pointX) {
                            //向右滑动
                            slider1.setViceProgress(pointX);
                        } else {
                            //向左滑动
                            slider1.setViceProgress(pointX);

                        }
                    }

                    break;
                case PRIMARY_POINT_UP:
                    id = 0;
                    slider1.setEnabled(true);
                    break;
            }
            return false;
        });

        Button button2 = (Button) findComponentById(ResourceTable.Id_button2);
        button2.setTouchEventListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case PRIMARY_POINT_DOWN:
                    slider2.setEnabled(false);
                    break;
                case POINT_MOVE:
                    pointX = FocusUtil.getWx(touchEvent) - 107;
                    button2.setContentPositionX(pointX);
                    slider2.setProgressValue(slider2.getProgress());
                    index = pointX;
                    mButtonX2 = pointX;
                    if (id == 0) {
                        id = index;
                    }
                    LogUtils.d("Darren","slider1.getProgress():" + slider2.getProgress());
                    if (pointX <= slider2.getProgress()) {
                        button2.setContentPositionX(694);
                        mButtonX2 = 694;
                        slider2.setViceProgress(694);
                    } else {
                        if (id > pointX) {
                            //向右滑动
                            slider2.setViceProgress(pointX);
                        } else {
                            //向左滑动
                            slider2.setViceProgress(pointX);

                        }
                    }

                    break;
                case PRIMARY_POINT_UP:
                    id = 0;
                    slider2.setEnabled(true);
                    break;
            }
            return false;
        });

        Button button3 = (Button) findComponentById(ResourceTable.Id_button3);
        button3.setTouchEventListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case PRIMARY_POINT_DOWN:
                    slider3.setEnabled(false);
                    break;
                case POINT_MOVE:
                    pointX = FocusUtil.getWx(touchEvent) - 107;
                    button3.setContentPositionX(pointX);
                    slider3.setProgressValue(slider3.getProgress());
                    index = pointX;
                    mButtonX3 = pointX;
                    if (id == 0) {
                        id = index;
                    }
                    LogUtils.d("Darren","slider1.getProgress():" + slider3.getProgress());
                    if (pointX <= slider3.getProgress()) {
                        button3.setContentPositionX(694);
                        mButtonX3 = 694;
                        slider3.setViceProgress(694);
                    } else {
                        if (id > pointX) {
                            //向右滑动
                            slider3.setViceProgress(pointX);
                        } else {
                            //向左滑动
                            slider3.setViceProgress(pointX);

                        }
                    }

                    break;
                case PRIMARY_POINT_UP:
                    id = 0;
                    slider3.setEnabled(true);
                    break;
            }
            return false;
        });

        slider1 = (Slider) findComponentById(ResourceTable.Id_slider1);
        slider1.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                    if (mButtonX == 0){
                        if (i >= 694) {
                            slider1.setProgressValue(174);
                        }
                    }else {
                        LogUtils.d("Darren","mButtonX:" + mButtonX);
                        LogUtils.d("Darren","i:" + i);
                        if (i >= mButtonX){
                            slider1.setProgressValue(174);
                        }
                    }
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });

        slider2.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                if (mButtonX2 == 0){
                    if (i >= 694) {
                        slider2.setProgressValue(174);
                    }
                }else {
                    LogUtils.d("Darren","mButtonX:" + mButtonX2);
                    LogUtils.d("Darren","i:" + i);
                    if (i >= mButtonX2){
                        slider2.setProgressValue(174);
                    }
                }
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });

        slider3.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                if (mButtonX3 == 0){
                    if (i >= 694) {
                        slider3.setProgressValue(174);
                    }
                }else {
                    LogUtils.d("Darren","mButtonX:" + mButtonX3);
                    LogUtils.d("Darren","i:" + i);
                    if (i >= mButtonX3){
                        slider3.setProgressValue(174);
                    }
                }

                LogUtils.d("Darren","onProgressUpdated i:" + i);
                String str = String.format("%.1f", (float) slider3.getProgress() / 20);
                progresstext3.setText(str);
                progresstext3.setContentPositionX(slider3.getProgress() * 1010 / 868);
            }

            @Override
            public void onTouchStart(Slider slider) {
                progresstext3.setVisibility(Component.VISIBLE);
            }

            @Override
            public void onTouchEnd(Slider slider) {
                progresstext3.setVisibility(Component.INVISIBLE);
            }
        });

    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("SeekBar");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
